/*
 * DropFrame.java
 *
 * Created on August 28, 2008, 1:48 PM
 */

package jturbofile.gui2;

import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author  jbk
 */
public class DropFrame extends javax.swing.JFrame {
    Rectangle activationArea = new Rectangle(0, 0, 10, 300);
    Point anchorPoint = new Point(0, 200);
    Point sizePoint;
    
    private DropFrame frame;

    /** Creates new form DropFrame */
    public DropFrame() {
        initComponents();
        this.setAlwaysOnTop(true);        
        frame = this;
        
        friendPanel.add(new FriendPanel(new Friend()));
        friendPanel.add(new FriendPanel(new Friend()));
        pack();
        new Thread(mouseWatcher).start();
        sizePoint = new Point(getWidth(), getHeight());
    }

    // Look at where the mouse is on the screen and make the window appear if it
    // hits the designated area.
    private Runnable mouseWatcher = new Runnable() {
        public void run() {
            boolean wasIn = false;
            while (true) {
                try {
                    Thread.sleep(100);
                    Point point = MouseInfo.getPointerInfo().getLocation();
                    boolean isIn = activationArea.contains(point) || (wasIn && frame.getBounds().contains(point));
                    if (wasIn != isIn) {
                        if (isIn) {
                            frame.fadeIn();                            
                        } else {
                            frame.fadeOut();
                        }
                    }
                    wasIn = isIn;
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }
    };

    private void fadeIn() {
        int width = (int)sizePoint.getX();
        int height = (int)sizePoint.getY();
        setBounds((int) anchorPoint.getX(), (int)anchorPoint.getY(), 0, 0);
        setVisible(true);
        for (int i = width; i>1; i=i/2){
            try {
                Thread.sleep(10);
            } catch (InterruptedException ex) {
                Logger.getLogger(DropFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
            setBounds((int)anchorPoint.getX(), (int)anchorPoint.getY(), width-i, height);
        }
    }

    private void fadeOut() {
        setVisible(false);
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        friendPanel = new javax.swing.JPanel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem5 = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenu4 = new javax.swing.JMenu();
        jRadioButtonMenuItem1 = new javax.swing.JRadioButtonMenuItem();
        jRadioButtonMenuItem2 = new javax.swing.JRadioButtonMenuItem();
        jRadioButtonMenuItem3 = new javax.swing.JRadioButtonMenuItem();
        jRadioButtonMenuItem4 = new javax.swing.JRadioButtonMenuItem();
        jCheckBoxMenuItem1 = new javax.swing.JCheckBoxMenuItem();
        jCheckBoxMenuItem2 = new javax.swing.JCheckBoxMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);
        setUndecorated(true);

        friendPanel.setLayout(new javax.swing.BoxLayout(friendPanel, javax.swing.BoxLayout.Y_AXIS));

        jMenu1.setText("Friends");

        jMenuItem1.setText("Add friend");
        jMenu1.add(jMenuItem1);

        jMenu3.setText("Remove friend");

        jMenuItem4.setText("Per Vers");
        jMenu3.add(jMenuItem4);

        jMenuItem5.setText("Someone else");
        jMenu3.add(jMenuItem5);

        jMenu1.add(jMenu3);
        jMenu1.add(jSeparator1);

        jMenuItem3.setText("Exit");
        jMenu1.add(jMenuItem3);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Configuration");

        jMenu4.setText("Compression");

        jRadioButtonMenuItem1.setSelected(true);
        jRadioButtonMenuItem1.setText("None");
        jMenu4.add(jRadioButtonMenuItem1);

        jRadioButtonMenuItem2.setSelected(true);
        jRadioButtonMenuItem2.setText("GZip (fast)");
        jMenu4.add(jRadioButtonMenuItem2);

        jRadioButtonMenuItem3.setSelected(true);
        jRadioButtonMenuItem3.setText("LZ");
        jMenu4.add(jRadioButtonMenuItem3);

        jRadioButtonMenuItem4.setSelected(true);
        jRadioButtonMenuItem4.setText("BZip2 (best)");
        jMenu4.add(jRadioButtonMenuItem4);

        jMenu2.add(jMenu4);

        jCheckBoxMenuItem1.setSelected(true);
        jCheckBoxMenuItem1.setText("Always connect");
        jCheckBoxMenuItem1.setToolTipText("Try to automatically connect to all friends at all times. \nIf disabled connections will be made only when transfering files.");
        jMenu2.add(jCheckBoxMenuItem1);

        jCheckBoxMenuItem2.setSelected(true);
        jCheckBoxMenuItem2.setText("Grab focus");
        jCheckBoxMenuItem2.setToolTipText("Should the program grab focus when an incoming file is completed?");
        jMenu2.add(jCheckBoxMenuItem2);

        jMenuItem2.setText("Activation gesture");
        jMenuItem2.setToolTipText("Configure how to make the droptarget window appear");
        jMenu2.add(jMenuItem2);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(friendPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(friendPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 128, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DropFrame(); //.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel friendPanel;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem1;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem1;
    private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem2;
    private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem3;
    private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem4;
    private javax.swing.JSeparator jSeparator1;
    // End of variables declaration//GEN-END:variables

}
